import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
import axios from 'axios'
import Amap from '@/components/amap/amap.vue'

Vue.component('Amap', Amap)

// 请求拦截器
axios.interceptors.request.use( 
  config => {
    let jwt=  localStorage.getItem('token')
    if(jwt){
      config.headers.token = jwt
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
axios.interceptors.response.use(
  response => {
    
    return response
  },
  error => {
    if (error.response.status === 401) {
      error.isHandled=true;
      ElementUI.Message.error('登录已过期，请重新登录')
      localStorage.removeItem('authorization')
      router.push('/login')
      return Promise.reject(error)
    }
   
  }
)

Vue.prototype.$axios = axios 
Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
